package com.ruoyi.web.controller.income;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.contarctbill.dto.PayIncomeDTO;
import com.ruoyi.income.domain.SumContarctIncome;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.income.domain.HouseContarctIncome;
import com.ruoyi.income.service.IHouseContarctIncomeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 计划详情Controller
 * 
 * @author hct
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/income/income")
public class HouseContarctIncomeController extends BaseController
{
    @Autowired
    private IHouseContarctIncomeService houseContarctIncomeService;

    /**
     * 查询计划详情列表
     */
    @PreAuthorize("@ss.hasPermi('income:income:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseContarctIncome houseContarctIncome)
    {
        startPage();
        List<HouseContarctIncome> list = houseContarctIncomeService.selectHouseContarctIncomeList(houseContarctIncome);
        return getDataTable(list);
    }

    /**
     * 导出计划详情列表
     */
    @PreAuthorize("@ss.hasPermi('income:income:export')")
    @Log(title = "计划详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseContarctIncome houseContarctIncome)
    {
        List<HouseContarctIncome> list = houseContarctIncomeService.selectHouseContarctIncomeList(houseContarctIncome);
        ExcelUtil<HouseContarctIncome> util = new ExcelUtil<HouseContarctIncome>(HouseContarctIncome.class);
        util.exportExcel(response, list, "计划详情数据");
    }

    /**
     * 获取计划详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('income:income:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseContarctIncomeService.selectHouseContarctIncomeById(id));
    }

    /**
     * 新增计划详情
     */
    @PreAuthorize("@ss.hasPermi('income:income:add')")
    @Log(title = "计划详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseContarctIncome houseContarctIncome)
    {
        return toAjax(houseContarctIncomeService.insertHouseContarctIncome(houseContarctIncome));
    }

    /**
     * 修改计划详情
     */
    @PreAuthorize("@ss.hasPermi('income:income:edit')")
    @Log(title = "计划详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseContarctIncome houseContarctIncome)
    {
        return toAjax(houseContarctIncomeService.updateHouseContarctIncome(houseContarctIncome));
    }

    /**
     * 删除计划详情
     */
    @PreAuthorize("@ss.hasPermi('income:income:remove')")
    @Log(title = "计划详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseContarctIncomeService.deleteHouseContarctIncomeByIds(ids));
    }


//    /**
//     * 支付账单接口
//     *      1.修改待付，支付，已付
//     *      2.添加每个费项的流水记录
//     * @param incomeId
//     * @param price
//     * @return
//     */
//    @PreAuthorize("@ss.hasPermi('income:income:pay')")
//    @Log(title = "支付账单", businessType = BusinessType.UPDATE)
//    @PostMapping(value = "/{incomeId}/{price}")
//    public AjaxResult payBillToIncome(Long incomeId,Double price){
//        String userName = getUsername();
//        Long userId = getUserId();
//        try {
//            Object o = houseContarctIncomeService.payCurrMonthBill(incomeId, price, userName, userId);
//            return AjaxResult.success("",o);
//        } catch (Exception e) {
//            return AjaxResult.error(e.getMessage());
//        }
//    }

    @PreAuthorize("@ss.hasPermi('income:income:pay')")
    @Log(title = "支付账单", businessType = BusinessType.UPDATE)
    @PostMapping("/payBillToIncome")
    public AjaxResult payBillToIncome(@RequestBody List<PayIncomeDTO> incomeList){
        String userName = getUsername();
        Long userId = getUserId();
        try {
            Object o = houseContarctIncomeService.payCurrMonthBill(incomeList, userName, userId);
            return AjaxResult.success("",o);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    @GetMapping("/sumIncome")
    public AjaxResult sumIncome(Long ty){
            HouseContarctIncome houseContarctIncome= new  HouseContarctIncome();
            houseContarctIncome.setFeetype(1);
            houseContarctIncome.setFeetypeid(ty);
            SumContarctIncome sumContarctIncome = houseContarctIncomeService.sumIncome(houseContarctIncome);
            return AjaxResult.success("",sumContarctIncome);
    }
}
